package com.qyhl.webtv.module_user.login.code.perfect;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.util.LoginUtils;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 完善资料界面
 */
@Route(path = ARouterPathConstant.PERFECT)
public class PerfectActivity extends BaseActivity implements PerfectContract.PerfectView {
    @Autowired(name = "code")
    String code;

    @Autowired(name = "phone")
    String phone;

    @BindView(R2.id.editinvitation)
    EditText editinvitation;
    @BindView(R2.id.editname)
    EditText editname;
    @BindView(R2.id.complete)
    Button complete;

    private LoadingDialog.Builder builder;
    private PerfectContract.PerfectPresenterModel presenterModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.login_activity_aerfect2;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        initView();
    }

    @Override
    protected void setListener() {

    }

    private void initView() {
        presenterModel = new PerfectPresenterModel(this);

        builder = new LoadingDialog.Builder(this);
        builder.setTitle("登录中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);

        editname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(editname.getText().toString())) {
                    complete.setEnabled(false);
                } else {
                    complete.setEnabled(true);
                }
            }
        });
    }

    @OnClick({R2.id.login_back, R2.id.complete})
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.login_back) {
            finish();
        } else if (i == R.id.complete) {
            if (StringUtils.containsEmoji(editname.getText().toString())) {
                Toasty.normal(this, "昵称含非法字符", Toast.LENGTH_SHORT).show();
            } else {
                builder.show();
                String invitation = editinvitation.getText().toString();
                presenterModel.goLogin(phone, code, editname.getText().toString(),
                        TextUtils.isEmpty(invitation) ? "" : invitation.trim());
            }

        } else {
        }
    }

    @Override
    public void loginError(String message) {
        builder.dismiss();
        Toasty.normal(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginSuccess(UserInfoBean bean) {
        builder.dismiss();
        Toasty.normal(this, "登录成功！", Toast.LENGTH_SHORT).show();
        LoginUtils.setloginSuccess(bean, ActionConstant.PHONELOGIN);
        setResult(777);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
